--Pindef model
--module(..., package.seeall)

local sys = require 'sys'
require 'UserDef' 

-----------------------------------------------
--				IO端口定义
----------------------------------------------

PIN_LED_GREEN   = 59    --LED
PIN_LED_YELLOW  = 58   
PIN_LED_RED     = 64    
PIN_LED_BLUE    = 54 

PIN_WATCHDOG    = 9     --pin.PC4       外置看门狗



PIN_BEEP        = 8     --pin.PE10


PIN_HandleAim    =  40   --手柄,"已对准"
PIN_HandleCW     =  41   --手柄,"正转"
PIN_HandleCCW    =  65   --手柄,"反转"
PIN_HandleStudy  =  66   --手柄,"学习"

PIN_CW_Limit    = 18    --pin.PE7       限位
PIN_CCW_Limit   = 19    --pin.PC7
PIN_Hot_Alarm   = 20    --pin.PC12      大风
PIN_Wind_Alarm  = 21    --pin.PC13      过热

PIN_BLUETOOTH_LINKSTATUS = 37   -- 蓝牙模块联机工作状态，1： BT24联机；0：蓝牙模块脱机


-----------------------------------------------
--				设置LED等IO端口,低电平亮
-----------------------------------------------
LED_Green    = gpio.setup(PIN_LED_GREEN, 0, gpio.PULLUP)
LED_Blue     = gpio.setup(PIN_LED_BLUE,  0, gpio.PULLUP)
LED_Red      = gpio.setup(PIN_LED_RED,   0, gpio.PULLUP)
LED_Yellow   = gpio.setup(PIN_LED_YELLOW,0, gpio.PULLUP)

--GPS_BT_SELECT   = gpio.setup(PIN_UART_GPS_BT_SELECT,   0, gpio.PULLUP)     --0: 选择GPS； 1：选择BT24

CPU_WDtick   = gpio.setup(PIN_WATCHDOG,   0, gpio.PULLUP)     --触发看门狗的信号

Beep         = gpio.setup(PIN_BEEP,   0, gpio.PULLUP)     --蜂鸣器，高触发 

WATCH_DOG    = gpio.setup(PIN_WATCHDOG,  0, gpio.PULLUP)    -- 外置看门狗


-----------------------------------------------
--		        手柄
-----------------------------------------------
gpio.setup( PIN_HandleCW, nil)      --手柄,"正转"
gpio.setup( PIN_HandleCCW, nil)     --手柄,"反转"
gpio.setup( PIN_HandleAim, nil)     --手柄,"已对准"
gpio.setup( PIN_HandleStudy, nil)   --手柄,"学习"

gpio.debounce( PIN_HandleCW,    50)    --防抖50ms
gpio.debounce( PIN_HandleCCW,   50)    --防抖50ms
gpio.debounce( PIN_HandleAim,   50)    --防抖50ms
gpio.debounce( PIN_HandleStudy, 50)    --防抖50ms


-----------------------------------------------
--		限位开关和报警端子
-----------------------------------------------
--		CCW限位开关状态变化，发布消息
gpio.setup(PIN_CW_Limit, nil, gpio.PULLUP )
gpio.debounce(PIN_CW_Limit, 200)        -- 防抖200ms
bit.CW_Limit = gpio.get(PIN_CW_Limit)     -- 0:正常 1： 异常

--		CCW限位开关状态变化，发布消息
gpio.setup(PIN_CCW_Limit, nil, gpio.PULLUP )
gpio.debounce(PIN_CCW_Limit, 200)    --防抖200ms
bit.CCW_Limit = gpio.get(PIN_CCW_Limit)     --0:正常 1： 异常

--		Hot_Alarm热告警状态变化，发布消息
gpio.setup(PIN_Hot_Alarm, nil, gpio.PULLUP)
gpio.debounce(PIN_Hot_Alarm, 200)    --防抖200ms
bit.Hot_Alarm = gpio.get(PIN_Hot_Alarm)     --0:正常 1： 异常

--		PIN_Wind_Alarm 大风告警状态变化，发布消息
gpio.setup(PIN_Wind_Alarm, nil, gpio.PULLUP)
gpio.debounce(PIN_Wind_Alarm, 200)    --防抖200ms
bit.Wind_Alarm = gpio.get(PIN_Wind_Alarm)     --0:正常 1： 异常



-----------------------------------------------
--				蓝牙模块连接状态变化，发布消息
-----------------------------------------------
gpio.setup(PIN_BLUETOOTH_LINKSTATUS, function(val) 
    sys.publish("BT24_CHANGED",val)
    log.info("Bluetooth","状态=",val,"(1:联机,0:脱机)")
end, gpio.PULLUP)
